//
//  ReplyLetterComeTableViewCell.m
//  MJApp
//
//  Created by 王亮 on 15/7/31.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "ReplyLetterComeTableViewCell.h"

@interface ReplyLetterComeTableViewCell ()

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property(nonatomic,strong)UIImageView *headImageView;
@property(nonatomic,strong)UILabel *conLabel;
@property(nonatomic,strong)UIImageView *backImageView;

@end

@implementation ReplyLetterComeTableViewCell

- (void)awakeFromNib {
    // Initialization code
    
    [self UI];
    self.backgroundColor = Color_Back_Gray;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)UI {
    
    
    CGFloat side = 26;
    UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, side * 2, side * 2)];
    headImageView.layer.cornerRadius = CGRectGetHeight(headImageView.frame) / 2;
    headImageView.clipsToBounds = YES;
    headImageView.image = [UIImage imageNamed:@"grzx_01"];
    [self addSubview:headImageView];
    CGPoint headCenter = headImageView.center;
    headCenter.x += side;
    headCenter.y = CGRectGetHeight(self.frame) / 2 + CGRectGetHeight(self.timeLabel.frame);
    headImageView.center = headCenter;
    self.headImageView = headImageView;
    
    CGRect frame = CGRectZero;
    frame.origin.x = CGRectGetMaxX(headImageView.frame) + side / 2;
    frame.size.height = 39;
    frame.size.width = 100;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.center = CGPointMake(imageView.center.x, headImageView.center.y);
    imageView.image = [UIImage imageNamed:@"grzx_0113"];
    [self addSubview:imageView];
    imageView.image = [imageView.image resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 20) resizingMode:UIImageResizingModeStretch];
    self.backImageView = imageView;

    
    UILabel *label = [[UILabel alloc] initWithFrame:imageView.bounds];
    label.text = @"sfsfsfwsfsfsafafasfafsfsfsaf";
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont systemFontOfSize:14];
    label.numberOfLines = 0;
    [imageView addSubview:label];
    self.conLabel = label;
}


- (void)setInfoDic:(NSDictionary *)infoDic {
    
    _infoDic = infoDic;
    self.conLabel.text = infoDic[@"content"];
    self.headImageView.center = CGPointMake(self.headImageView.center.x, [infoDic[@"cellHigh"] floatValue] / 2);
    NSString *contect = infoDic[@"content"];
    CGSize size = [contect sizeWithFont:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(250, CGFLOAT_MAX)];
    CGRect frame = self.backImageView.frame;
    frame.size.height = size.height + 20;
    frame.size.width = size.width + 20;
    self.backImageView.frame = frame;
    
    self.backImageView.center = CGPointMake(self.backImageView.center.x, self.headImageView.center.y);
    self.conLabel.frame = CGRectMake(10, 5, frame.size.width - 10, frame.size.height - 10);

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kUrl,infoDic[@"f_head"]]];
    [self.headImageView sd_setImageWithURL:url];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *createDate = [formatter dateFromString:infoDic[@"create_time"]];
    NSTimeInterval timeInt = fabs([createDate timeIntervalSinceNow]);
    
    NSString *pre = @"秒钟";
    if (timeInt >= 60) {
        
        timeInt /= 60;
        pre = @"分钟";
    }
    if (timeInt >= 60) {
        
        timeInt /= 60;
        pre = @"小时";
    }
    if (timeInt >= 24) {
        
        timeInt /= 24;
        pre = @"天";
    }
    if (timeInt >= 10 && [pre isEqual:@"天"]) {
        
        self.timeLabel.text = infoDic[@"create_time"];
        
    }else {
        
        self.timeLabel.text = [NSString stringWithFormat:@"%d%@前",(int)timeInt,pre];
    }
}

@end
